from django.shortcuts import render, HttpResponse, redirect
from utils.tencent.cos import delete_bucket
from web import models

def setting(request, project_id):
    return render(request, 'web/setting.html')


def delete(request, project_id):
    """删除项目"""
    if request.method == 'GET':
        return render(request, 'web/setting_delete.html')
    project_name = request.POST.get('project_name')
    if not project_name or project_name != request.tracer.project.name:
        return render(request, 'web/setting_delete.html', {'error': "项目名错误"})

    # 项目名对了，进行删除（只有创建者可以删除）
    if request.tracer.user != request.tracer.project.creator:
        return render(request, 'web/setting_delete.html', {'error': "只有项目创建者可以删除项目"})

    # 1 删除桶
    # 第一步 删除桶的所有文件（找到桶所有文件 + 删除文件）
    #       删除桶的所有碎片文件（找到桶所有碎片文件 + 删除碎片）
    # 第二步 去删除桶

    # 2 删除项目

    delete_bucket(request.tracer.project.bucket, request.tracer.project.region)
    models.Project.objects.filter(id=request.tracer.project.id).delete()
    # 视频里用的是 redirect
    return redirect("web_project_list")
    # return render(request, 'web/project_list.html')
